<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\CategoryApi;
use Shop\Api\CategorySkuApi;
use Shop\Api\SkuApi;
use Shop\Api\SkuLangApi;
use Shop\Api\SkuvalueApi;
use Think\Exception;

class CategorySkuController extends AdminController{
	
	protected $cate_id;
	
	protected function _initialize(){
		parent::_initialize();
        $this->cate_id = I('get.cate_id',0);
        if(empty($this->cate_id))
        $this->cate_id = I('post.cate_id',0);
		
		$this->assign("cate_id",$this->cate_id );
	}

	public function index(){

        $lang = I('lang','zh-cn');
		$map = array(
			'cs.cate_id'=>$this->cate_id
		);

		$params = array(
			'cate_id'=>$this->cate_id
		);

		$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));

		$result = apiCall(CategoryApi::GET_INFO, array(array("id"=>$this->cate_id)));
		
		if(!$result['status']){
			$this->error($result['info']);
		}

		$cate_vo = $result['info'];
		//
		$result = (new CategorySkuApi())->queryJoin($this->cate_id,$page,$params);
		//
		if($result['status']){
		    $this->assign('lang',$lang);
            $this->assign("lang_list",LangApi::getLangSupport());
			$this->assign('cate_id',$this->cate_id);
			$this->assign('cate_vo',$cate_vo);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}

	public function bulkDelete(){
        $ids = $this->_post('ids',[]);
        if(empty($ids)) $this->error("请先选择要操作的项");

        $result = (new CategorySkuApi())->delete(['id'=>['in',$ids]]);
        $this->exitIfError($result);
        $this->success("操作成功");
    }

	public function bulkAdd(){
	    $ids = $this->_post('ids',[]);
        if(empty($ids)) $this->error("请先选择要操作的项");
        if(count($ids) >= 10){
            $this->error("由于系统限制一个类目只能关联10个规格");
        }
        $result = (new CategorySkuApi())->count(['cate_id'=>$this->cate_id]);
        if($result['status']){
            $count = intval($result['info']);
            if($count >= 10){
                $this->error("由于系统限制一个类目只能关联10个规格");
            }
        }

        $allEntity = [];
        foreach ($ids as $id){
            array_push($allEntity,['cate_id'=>$this->cate_id,'sku_id'=>$id]);
        }
        try{
            $result = (new CategorySkuApi())->addAll($allEntity);
            if(!$result['status']){
                $this->error($result['info']);
            }
            $this->success("操作成功");
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
	}
	
	public function add(){
        $result = apiCall(CategoryApi::GET_INFO, array(array("id"=>$this->cate_id)));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $cate_vo = $result['info'];
        $params = array(
            'cate_id'=>$this->cate_id
        );
        $page = array('curpage' => I('get.p', 0), 'size' => 30);
        //
        $result = (new CategorySkuApi())->queryRightJoin($this->cate_id,$page,$params);

        $this->assign('cate_vo',$cate_vo);
        $this->assign('list',$result['info']['list']);
        $this->assign('show',$result['info']['show']);
        $this->display();
	}


	public function delete(){
		$id = I('get.id',0);
		if($id <= 0) $this->error("缺失id");
		$result = (new CategorySkuApi())->delete(['id'=>$id]);
		if($result['status']){
		    $this->success('删除成功',U('CategorySku/index',['cate_id'=>$this->cate_id]));
        }
	}

    /**
     * 类目规格关联
     */
	public function relation(){
	    $name = I('post.name','');
	    $map = ['level'=>3];
        $params = [];
        if(!empty($name)){
            $map['name'] = ['like','%'.$name.'%'];
            $params['name'] = $name;
        }

        $page = array('curpage' => I('get.p', 0), 'size' => 30);
        $order = " display_order desc ";
        //
        $result = apiCall(CategoryApi::QUERY,array($map,$page,$order,$params));

        if($result['status']){
            $this->assign('name',$name);
            $this->assign('show',$result['info']['show']);
            $this->assign('list',$result['info']['list']);
            $this->display();
        }else{
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }
	
}
